package com.vr.caricature.main;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.widget.ImageView;

import com.vr.caricature.R;
import com.vr.caricature.common.MainPageAdapter;
import com.vr.caricature.fenlei.FenleiFragment;
import com.vr.caricature.geren.GerenFragment;
import com.vr.caricature.local.LocalVideoFragment;
import com.vr.caricature.tuijian.TuijianFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.InjectViews;
import butterknife.OnClick;

public class MainActivity extends FragmentActivity implements ViewPager.OnPageChangeListener{


    @InjectView(R.id.vp_main) ViewPager vpMain;
    @InjectViews({R.id.iv_tuijian,R.id.iv_fenlei,R.id.iv_bendi,R.id.iv_geren}) List<ImageView> imageViews;

    private FragmentManager fm;
    private MainPageAdapter mVpAdapter;
    private List<Fragment> mFragments;

    private int prePosition = 0;

    private TuijianFragment tuijianFragment;
    private FenleiFragment fenleiFragment;
    private LocalVideoFragment localVideoFragment;
    private GerenFragment gerenFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.inject(this);
        initViews();

    }

    public void initViews(){
        fm = getSupportFragmentManager();
        tuijianFragment = new TuijianFragment();
        fenleiFragment = new FenleiFragment();
        localVideoFragment = new LocalVideoFragment();
        gerenFragment = new GerenFragment();
        mFragments = new ArrayList<>();
        mFragments.add(tuijianFragment);
        mFragments.add(fenleiFragment);
        mFragments.add(localVideoFragment);
        mFragments.add(gerenFragment);
        mVpAdapter = new MainPageAdapter(fm,mFragments);
        vpMain.setAdapter(mVpAdapter);

        vpMain.setCurrentItem(0);
        vpMain.addOnPageChangeListener(this);
    }

    @OnClick(R.id.rl_tuijian)
    public void tuijian(){
        vpMain.setCurrentItem(0);
    }

    @OnClick(R.id.rl_fenlei)
    public void fenlei(){
        vpMain.setCurrentItem(1);
    }

    @OnClick(R.id.rl_bendi)
    public void bendi(){
        vpMain.setCurrentItem(2);
    }

    @OnClick(R.id.rl_geren)
    public void geren(){
        vpMain.setCurrentItem(3);
    }


    public void setItemSelect(int position){
        vpMain.setCurrentItem(1);
        if(fenleiFragment!=null){
            fenleiFragment.setCurrentItem(position);
        }
    }


    @Override
    public void onPageSelected(int position) {
        switch (prePosition){
            case 0:
                imageViews.get(prePosition).setBackgroundResource(R.mipmap.tuijian);
                break;
            case 1:
                imageViews.get(prePosition).setBackgroundResource(R.mipmap.fenlei);
                break;
            case 2:
                imageViews.get(prePosition).setBackgroundResource(R.mipmap.bobendi);
                break;
            case 3:
                imageViews.get(prePosition).setBackgroundResource(R.mipmap.geren);
                break;
        }
        switch (position){
            case 0:
                imageViews.get(position).setBackgroundResource(R.mipmap.htuijian);
                break;
            case 1:
                imageViews.get(position).setBackgroundResource(R.mipmap.hfenlei);
                break;
            case 2:
                imageViews.get(position).setBackgroundResource(R.mipmap.hbobendi);
                break;
            case 3:
                imageViews.get(position).setBackgroundResource(R.mipmap.hgeren);
                break;
        }
        prePosition = position;
    }


    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
